import {Schema} from 'mongoose';

const {SchameTypes} = require('mongoose'),
	mongoosePaginate = require('mongoose-paginate');

module.exports = class extends think.Mongoose {
	get schema() {
		const schema = new Schema({
			account: String,
			password: String,
			name: String,
			sex: Number,
			age: {type: Number, min: 0},
			birthday: Date,
			status: Number,
			createTime: Date,
			updateTime: Date
		}, {
			versionKey: false
		});
		schema.plugin(mongoosePaginate);
		return schema;
	}

	/**
	 * @author: ares
	 * @date: 2019/4/3 10:03
	 * @description: 新建
	 * @param {object}data
	 * @returns {Promise.<*>}
	 */
	async $create(data) {
		data.password = 'e10adc3949ba59abbe56e057f20f883e';
		data.birthday = think.datetime(data.birthday, 'YYYY-MM-DD');
		data.createTime = think.datetime(+new Date);
		let find = await this.find({account: data.account});
		if (think.isEmpty(find)) return await this.create(data);
		return null;
	}

	/**
	 * @author: ares
	 * @date: 2019/4/3 11:50
	 * @description: 删除
	 * @param {string}_id
	 * @returns {Promise.<*>}
	 */
	async $delete(_id) {
		let del = await this.deleteOne({_id});
		return del.deletedCount;
	}

	/**
	 * @author: ares
	 * @date: 2019/4/3 11:29
	 * @description: 更新
	 * @param {object}data
	 * @returns {Promise.<*>}
	 */
	async $update(data) {
		if (data.birthday) data.birthday = think.datetime(data.birthday, 'YYYY-MM-DD');
		data.updateTime = think.datetime(+new Date);
		let upd = await this.update({_id: data._id}, data);
		return upd.nModified;
	}

	/**
	 * @author: ares
	 * @date: 2019/4/3 11:37
	 * @description: 切换用户启用禁用状态
	 * @param data
	 * @returns {Promise.<*>}
	 */
	async toggleState(data) {
		data.updateTime = think.datetime(+new Date);
		let upd = await this.update({_id: data._id}, {status: data.status, updateTime: think.datetime(+new Date)});
		return upd.nModified;
	}

	/**
	 * @author: ares
	 * @date: 2019/4/3 11:39
	 * @description: 重置用户密码
	 * @param {object}data 用户信息
	 * @returns {Promise.<*>}
	 */
	async resetPwd(data) {
		let upd = await this.update({_id: data._id}, {
			password: 'e10adc3949ba59abbe56e057f20f883e',
			updateTime: think.datetime(+new Date)
		});
		return upd.nModified;
	}

	/**
	 * @author: ares
	 * @date: 2019/4/10 16:44
	 * @description: 用户修改自己的密码
	 * @param {string}_id
	 * @param {string}oldPwd
	 * @param {string}newPwd
	 * @returns {Promise.<*>}
	 */
	async modifyPwd(_id, oldPwd, newPwd) {
		let find = await await this.find({_id});
		if (find.length == 0) return {error: '账号不存在'};
		if (find[0].password !== oldPwd) return {error: '原始密码错误'};
		let upd = await this.update({_id, password: oldPwd}, {password: newPwd, updateTime: think.datetime(+new Date)});
		return upd.nModified;
	}

	/**
	 * @author: ares
	 * @date: 2019/4/9 13:40
	 * @description: 分页查询,可搜索
	 * @param {string}searchKey   关键字
	 * @param {number}currentPage
	 * @param {number}pageSize
	 * @returns {Promise.<*>}
	 */
	async getPageList(searchKey, currentPage, pageSize) {
		let reg = new RegExp(searchKey, 'ig');
		return await this.paginate({$or: [{name: {$regex: reg}}, {account: {$regex: reg}}]}, {
			page: currentPage,
			limit: pageSize
		});
	}

	/**
	 * @author: ares
	 * @date: 2019/4/9 14:43
	 * @description: 根据id获取信息
	 * @param {string}_id
	 * @returns {Promise.<*>}
	 */
	async getById(_id) {
		return await this.findOne({_id});
	}

	/**
	 * @author: ares
	 * @date: 2019/4/9 14:47
	 * @description: 根据账号获取用户信息
	 * @param {string}account
	 * @returns {Promise.<*>}
	 */
	async getByAccount(account) {
		return await this.findOne({account});
	}

	/**
	 * @author: ares
	 * @date: 2020/3/8 15:21
	 * @description: 获取登录用户信息
	 * @param {string}account
	 * @param {string}password
	 * @returns {Promise.<*>}
	 */
	async getLoginInfo(account, password) {
		return await this.findOne({account, password});
	}

	/**
	 * @author: ares
	 * @date: 2019/4/10 11:04
	 * @description: 记录登录/登出信息
	 * @param {object}data
	 * @param {boolean}logout  is logout
	 * @returns {Promise.<*>}
	 */
	async recordLogin(data, logout) {
		let loginInfoModel = this.mongoose('loginInfo'),
			result = logout ? await loginInfoModel.updateOne({token: data.token}, data, {upsert: true})
				: await loginInfoModel.updateOne({userId: data.userId}, data, {upsert: true});
		return result;
	}
}